package com.album.controller;


import cn.hutool.http.HttpResponse;
import com.album.auto.JSONAuthentication;
import com.album.entity.AlUser;
import com.album.mapper.AlUserMapper;
import com.album.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author album
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/al-user")
public class AlUserController extends JSONAuthentication {
    @Autowired
    AlUserMapper alUserMapper;
    @ApiOperation(value = "添加用户")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("role") String role) throws Exception {
        AlUser shUser = new AlUser();
        shUser.setUsername(username);
        shUser.setPassword(password);
        shUser.setRole(role);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", username);
       if(alUserMapper.selectOne(wrapper)!=null){
           Result error = Result.error().message("用户已存在");
           this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除用户")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = alUserMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改密码")
    @PostMapping("/updpassword")
    public void updpassword(HttpServletRequest request, HttpServletResponse response, @RequestParam("password") String password, @RequestParam("rpassword") String rpassword, @RequestParam("id") int id) throws Exception {
        AlUser anUser1 = alUserMapper.selectById(id);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password1 = new BCryptPasswordEncoder().encode(rpassword);
        if (encoder.matches(password, anUser1.getPassword())) {
            anUser1.setPassword(password1);
            int i = alUserMapper.updateById(anUser1);
            if (i > 0) {
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        } else {
            Result error = Result.error();
            error.setMessage("密码不正确");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过用户id查找用户")
    @PostMapping("/findById")
    public void findById(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {

        AlUser list = alUserMapper.selectById(id);
        if (list != null) {
            Result ok = Result.ok().data("user", list);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有此用户");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "通过名字查找用户")
    @PostMapping("/findByname")
    public void findByname(HttpServletRequest request, HttpServletResponse response, @RequestParam("username") String username, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("username", username);
        Page<AlUser> page = new Page<>(size, current);
        Page<AlUser> pages = alUserMapper.selectPage(page, wrapper);
        List<AlUser> list = pages.getRecords();
        int total = alUserMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有此用户");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部用户")
    @PostMapping("/findAll")
    public void findBynamea(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {

        Page<AlUser> page = new Page<>(current, size);
        Page<AlUser> pages = alUserMapper.selectPage(page, null);
        List<AlUser> list = pages.getRecords();
        int total = alUserMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有用户");
        this.WriteJSON(request, response, error);
    }

}

